package com.gxa.ehome.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "PayServlet",urlPatterns = {"/pay"})
public class PayServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String URL = "https://openapi.alipaydev.com/gateway.do";
        String APP_ID = "2016101500691047";
        String APP_PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGYsfJwezJJ29YFuDzSn5DpbESTUxApmxnJKrgnGCic7Nt6m8UHnl+q7JFlt1TJUu+VeUmHp8fGs+LHEeIUtUFfWGrqbHpq3deAbS2b7eBCs9U0+ObtDFRcZbGdBK1fuLwQwH2Fqa+25EDuNIWZWaKdICQ9KjeLx21SN1aVg46udf5Z1XdGXiL6sc4e8qYnRhlzKvAy7+3O199I6fmT8c3GJiUfk5M1zST9nWM/DStUUM4KMVY0VkCaGwc1vSkrNW6m6ukY9B9KuA1ASX32goKSh7JQLEkcRC94pJJctnRIGJdODWViFE2xIbdOo/7ylGwBD8unmCDmhgEbwVnryAjAgMBAAECggEBAIWmVAAJx44+k/PNvSb5HR31+Wa0TXO2j/T8eJ0tqMWnv/k+v/4IWNhrtb6TzcXVP8Ep992zcpkituoo09N2Xbk5+xsYh4JXLz3lAG64KI5Dy/MXUDQYPzw6dbMk2T571QMSAS9XU7pY7539/6KaciKcvwhq6k8eGLOkpdAj6b7oeS1MaosLNf1WEYtpz6DlLTuavs0Js6QL5r/LugOvNmW+7HkQ+M+LKHEblt41iFj+tJiX/Zn2jSG0q8IzKrXipwDXv1oMnPqb3iyCcCYvbArxGYRM6kP4bKPn254XZlBcusiiMy7xydCaB4b2ErmgNK16QgG/hXwR0J9ioSi8odECgYEA9pkfkv6CfFN/raEe49vaTFxDt94Njw6jzeH95NK1LnyowWkkcT7S/bFtx71jNhRMFLDUzc5UkV1iPlAWhhaY0e89BFg4oJv3A035SaFxcNb/xwEcfcg9DgkQqSU+ZxXdDBnsU7PVYiVGxE4rIJHJH7zxAIGgyY4Su3HccYhIX30CgYEAi4Jt7GUzegfPEIEshAgFPNJ5+opD9Qt6T+WPWOu9n6EhIOBvp6bR7pUocUEdVx5IfQ/aTTRUI7eGP26JwS6TGJ7/2ogE61YghxSjBqr7TB8jBFQJZ1H7Bmepgx+W+hzSuaLmmGvTH1KU8NjM9MLIaj4dPy2wCUXMUE43vEqO0B8CgYBAhfhZAfcqiL6Ts+DdRh1sdYfSiGuLzwzVRhik0u1NaJjPYq+CYina754F8WwQLmdp0osb/qzqDEE0hMgf9bGu3KZeiKbof2a2mqRru65vFBKBA61szAQNtBmD6JbXakUYcYAAtsFSmOZXXwyxzPOPNmTo+QIVOvNz0GsZ2cr2EQKBgGeGlX3cJ5QB4XW09czQitp0slHqcr2QE+bgSjzJS4D+uC/GfPTjRVwP0eAhWt2AOnib3/p0v491rCi8JXt2zW9y5hhL4aKl2iODe+sBzNPTM1AN8KB8ZJR/LnqAYbAe3X0mVop7UUMoLCCCMa3cMY8totR0WfK3mgNp19BhX5MvAoGAN+vzlRCut9T3zsR79Ylw/75fFzWO4L/b6YqgQm9gsPNQXoAXOLi7WuSRZpmahZp7aeJn2V+T9s5tWuGg6JopLwxm/T0z3jgoH9L4A52TQej882SOJKmwDIxxL0JXVFOK6MAp1YfPlKPkjczdLY8Qx6zv5hxURgzFT4HdIeLugWM=";
        String FORMAT = "json";
        String CHARSET = "UTF-8";
        String ALIPAY_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkLSJI5fHSxylMEWExMeAvM+kS5sAsOxjMygS/D42ygDpXV8g1YYNYLML4dcuxLbX8/GOG5YFuFINy7YBOGS0q8/avcOAJRXbdmUf0ukhUjz5+OEmVjhjJ991Km8VGojpPdaoIOlXYRNwvv0oxbyNbOnlzE+NJd+O/8bST688ufU4v2yfd5Jsg3okCXuRFjSfBL50HyztSwMFAsWwd9t72nSbx5hkhTYkcNhJ8Hxg7p0QJJIo+ppA0p4ooGkoyCDv0UXDGrlHfRwELj4WHCnyBFUpmWgba0og73fr3gqbcB5lWIzrcpx8jYY5h6isfRNwiy+FlQLivXyQlzc3jW4REwIDAQAB";
        String SIGN_TYPE = "RSA2";

        AlipayClient alipayClient = new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:8080/crm/html/user/paySuccess.html"); // 返给用户的响应地址,回调结果
//        alipayRequest.setReturnUrl("http://localhost:8080/crm/success.jsp"); // 返给用户的响应地址,回调结果
        alipayRequest.setNotifyUrl("http://localhost:8080/crm/payReturn");//在公共参数中设置回跳和通知地址

        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+ UUID.randomUUID().toString().replace("-","") +"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":999999," +
                "    \"subject\":\"Iphone6 16G\"," +
                "    \"body\":\"Iphone6 16G\"," +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\"2088511833207846\"" +
                "    }"+
                "  }");//填充业务参数

        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + CHARSET);
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);

    }
}
